package team.cdlzz.service;

import team.cdlzz.config.BeanConfig;
import team.cdlzz.controller.UpWebSocketController;
import team.cdlzz.dao.UserMapper;
import team.cdlzz.model.pojo.User;
import team.cdlzz.model.servicePojo.Email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import team.cdlzz.model.servicePojo.SocketUser;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zw
 * @date 2022-06-19.
 */
@Service
public class UtilService {
    @Resource  //导入资源
    private JavaMailSender javaMailSender; //注入

    public static ConcurrentHashMap<String ,String> mailString = new ConcurrentHashMap<>();

    @Autowired
    Email email;

    @Autowired
    UserMapper userMapper;

//    public Map<Integer,Integer> getRoomList() {
//        ConcurrentHashMap<Integer, ArrayList<SocketUser>> mapList = UpWebSocketController.mapList;
//        Map map = new HashMap();
//        mapList.forEach();
//        return list;
//    }

    /**
     * 发送验证码
     */
    public String sendRomeStr(String mail){
//
        if (UserExist(mail)){
            return "该邮箱已被使用";
        }else{
            String str = RandomStr();
            System.out.println(str+"===="+mail);
            mailString.put(mail,str);
            boolean b = sendEmail(mail, str);
            if (b)
                return "发送成功";
            else
                return "发送失败";
//            return "接口未开放,目前用于测试 ---- " +str;
        }
    }

    /**
     * 产生4为随机数验证码
     */
    public static String RandomStr(){    //随机产生字母串
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWSYZ0123456789";    //可随机产生的字符
        Random random = new Random();
//        StringBuffer sb = new StringBuffer();
        char[] s = new char[4];
        for(int i=0;i<4;i++){
            int number=random.nextInt(62);
            s[i] = str.charAt(number);
        }
        return String.copyValueOf(s);
    }

    public boolean sendEmail(String mailFrom,String rom) {
        boolean flag = false;
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            mimeMessageHelper.setFrom(new InternetAddress(email.getMailTo(), "标题", "UTF-8")); //设置发件人
            mimeMessageHelper.setTo(mailFrom); //设置收件人
            mimeMessageHelper.setSubject(email.getSubject()); //设置邮件标题
            mimeMessageHelper.setText(email.getContent() +  rom, true); //设置邮件内容
            javaMailSender.send(mimeMessage);
            flag =true;
        } catch (MessagingException | UnsupportedEncodingException e) {
            e.printStackTrace();
            flag = false;
        }
        finally {
            return flag;
        }
    }

    public boolean UserExist(String email){
        User user  = userMapper.UserEmailExist(email);
        return user != null;
    }
}
